package com.gklee.regionselector;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity
{

    private List<SelectBean> provinceBeans;
    private String mProvince;
    private String mCity;
    private String mZone;
    private RegionSelectDialog regionSelectDialog;
    private String mArea;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView tv_region = findViewById(R.id.tv_region);

        //初始化时，传入RegionLevel设置三级联动or四级联动）
        regionSelectDialog = new RegionSelectDialog(this, RegionLevel.LEVEL_FOUR);
//        regionSelectDialog.setLevel();
        //获取省级数据
        provinceBeans = new ArrayList<>();
        provinceBeans.addAll(DataProvider.resetSelectBean());
        //此步骤必须，否则无法显示
        regionSelectDialog.setOnRegionDataSetListenr(new OnRegionDataSetListener()
        {

            //必须有返回值
            @Override
            public List<SelectBean> setProvinceList()
            {
                return provinceBeans;
            }

            //必须有返回值
            @Override
            public List<SelectBean> setOnProvinceSelected(SelectBean SelectBean)
            {

                //接收选中的省份
                mProvince = SelectBean.getName();
                //返回显示的城市
                List<SelectBean> cityBeans = new ArrayList<>();
                if(SelectBean.hasChild())
                    cityBeans.addAll(SelectBean.getChildBeans());
                return cityBeans;
            }

            //必须有返回值
            @Override
            public List<SelectBean> setOnCitySelected(SelectBean SelectBean)
            {
                //接收选中的城市
                mCity = SelectBean.getName();
                //返回显示的区县
                List<SelectBean> zoneBeans = new ArrayList<>();
                if(SelectBean.hasChild())
                    zoneBeans.addAll(SelectBean.getChildBeans());
                return zoneBeans;
            }

            //四级联动时，必须有返回值。三级联动时，可忽略。
            @Override
            public List<SelectBean> setOnZoneSelected(SelectBean SelectBean)
            {
                //接收选中的区县
                mZone = SelectBean.getName();

                List<SelectBean> zoneList = new ArrayList<>();
                if(SelectBean.hasChild())
                    zoneList.addAll(SelectBean.getChildBeans());
                //返回显示的街道
                //（如果是三级联动，则不需要返回）
                return zoneList;
            }

            @Override
            public void setOnAreaSelected(SelectBean SelectBean)
            {
                //如果是四级联动，则得到选中的街道,如果是三级联动，则没有返回
                mArea = SelectBean.getName();
                tv_region.setText(tv_region.getText() + "\r\n" + mProvince + " " + mCity + " " + mZone + " " + mArea);
            }

            @Override
            public void updateSelectedList(List<SelectBean> beans)
            {
                Log.d("MainActivity", beans.toString());
            }

            @Override
            public void resultSelectedList(List<SelectBean> beans)
            {
                Log.d("MainActivity", beans.toString());
            }

        });
        tv_region.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                //显示
                regionSelectDialog.showDialog();
            }
        });

    }
}
